package and.calc;

import android.R.integer;
import android.util.Log;




public class Calculator {

	float m_number1;
	float m_number2;
	float m_result;
	Operations operation;
	
	
	
	
	/**
	 * 
	 */
	public Calculator() {
	
		m_number1 = 0;
		m_number2 = 0;
		m_result = 0;
		operation = Operations.none;
	}
	
	// Setters For (Number1,Number2,Operation)
	public void setM_number1(float m_number1) {
		this.m_number1 = m_number1;
	}
	public void setM_number2(float m_number2) {
		this.m_number2 = m_number2;
	}
	
	
	// Setters with Format
	public void setM_number1WithFormat(String number1,Format format)
	{
		String numberAfterFormat = number1;
		
		switch(format)
		{
		case HEX:
			numberAfterFormat = convertHEXtoDEC(number1);
			break;
			
		case DEC:
			numberAfterFormat = number1;
			break;
			
		case BIN:
			numberAfterFormat = convertBINtoDEC(number1);
			break;
			
		case OCT:
			numberAfterFormat = convertOCTtoDEC(number1);
			break;
			
		}
		
		
		m_number1 = Float.parseFloat(numberAfterFormat);
		
	}
	
	
	public void setM_number2WithFormat(String number2,Format format)
	{
		String numberAfterFormat = number2;
		
		switch(format)
		{
		case HEX:
			numberAfterFormat = convertHEXtoDEC(number2);
			break;
			
		case DEC:
			numberAfterFormat = number2;
			break;
			
		case BIN:
			numberAfterFormat = convertBINtoDEC(number2);
			break;
			
		case OCT:
			numberAfterFormat = convertOCTtoDEC(number2);
			break;
			
		}
		
		
		m_number2 = Float.parseFloat(numberAfterFormat);		
	}
	
	
	
	public void setOperation(Operations op)
	{
		operation = op;
	}
	
	
	// Get the results
	public float getM_result() 
	{
		switch(operation)
		{
			case add:
				addNumbers();
				break;
			
			case sub:
				subNumbers();
				break;
				
			case mul:
				mulNumbers();
				break;
				
			case div:
				divNumbers();
				break;
							
			default:
				m_result = m_number2;
				break;
		}
					
		return m_result;
	}
	
	
	public String getM_resultWithFormat(Format format)
	{
		String result;
	
		Log.d("Calculator", "before Setting Operation");
		
		switch(operation)
		{
			case add:
				addNumbers();
				break;
			
			case sub:
				subNumbers();
				break;
				
			case mul:
				mulNumbers();
				break;
				
			case div:
				divNumbers();
				break;
						
			case and:
				andNumbers();
				break;
				
			case or:
				orNumbers();
				break;
				
			case xor:
				xorNumbers();
				break;
				
			case not:
				notNumbers();
				break;
				
			default:
				m_result = m_number2;
				break;
		}
		
		Log.d("Caculator", "After Operation executed");
		
		result = String.valueOf(Math.round(m_result)).toUpperCase();
		
		Log.d("Caculator", "After converting result to String");
		
		switch(format)
		{
		case HEX:
			result = convertDECtoHEX(result);
			break;
			
		case BIN:
			result = convertDECtoBIN(result);
			break;
			
		case OCT:
			result = convertDECtoOCT(result);
			break;
			
		}
		
		
		Log.d("Caculator", "After converting to the number format");
		
		return result.toUpperCase();
	}
		
	// Operations Implementations
	private void addNumbers()
	{
		m_result = m_number1 + m_number2;
	}
	
	private void subNumbers()
	{
		m_result = m_number1 - m_number2;
	}
	
	private void divNumbers()
	{
		m_result = m_number1 / m_number2;		
	}
	
	private void mulNumbers()
	{
		m_result = m_number1 * m_number2;
	}
	
	private void andNumbers()
	{
		m_result = ((int)m_number1) & ((int)m_number2) ;
	}
	
	private void orNumbers()
	{
		m_result = ((int)m_number1) | ((int)m_number2) ;
	}
	
	private void xorNumbers()
	{
		m_result = ((int)m_number1) ^ ((int)m_number2) ;
	}

	private void notNumbers()
	{
		m_result = ~((int)m_number2) ;
	}
	
	
	/// Conversion Functions "Convert between Different Formats
	/// 1- Convert From DECIMAL
	public String convertDECtoHEX(String a_number)
	{
		
		int number =  Integer.parseInt(a_number,10);
		String l_newNumber = Integer.toHexString(number);
				
		return l_newNumber;
	}
	
	public String convertDECtoOCT(String a_number)
	{
		int number =  Integer.parseInt(a_number,10);
		String l_newNumber = Integer.toOctalString(number);
				
		return l_newNumber;
	}
	
	public String convertDECtoBIN(String a_number)
	{
		int number =  Integer.parseInt(a_number,10);
		String l_newNumber = Integer.toBinaryString(number);
				
		return l_newNumber;
	}
	
	/// 2- Convert From BIN
	public String convertBINtoHEX(String a_number)
	{
		int number =  Integer.parseInt(a_number,2);
		String l_newNumber = Integer.toHexString(number);
				
		return l_newNumber;
	}
	
	public String convertBINtoOCT(String a_number)
	{
		int number =  Integer.parseInt(a_number,2);
		String l_newNumber = Integer.toOctalString(number);
				
		return l_newNumber;
	}
	
	public String convertBINtoDEC(String a_number)
	{
		int number =  Integer.parseInt(a_number,2);
		String l_newNumber = Integer.toString(number);
				
		return l_newNumber;
	}
	
	/// 3- Convert From HEXDECIMAL
	public String convertHEXtoDEC(String a_number)
	{
		int number =  Integer.parseInt(a_number,16);
		String l_newNumber = Integer.toString(number);
				
		return l_newNumber;
	}
	
	public String convertHEXtoOCT(String a_number)
	{
		int number =  Integer.parseInt(a_number,16);
		String l_newNumber = Integer.toOctalString(number);
				
		return l_newNumber;
	}
	
	public String convertHEXtoBIN(String a_number)
	{
		int number =  Integer.parseInt(a_number,16);
		String l_newNumber = Integer.toBinaryString(number);
				
		return l_newNumber;
	}
	
	/// 4- Convert From OCTAL
	public String convertOCTtoHEX(String a_number)
	{
		int number =  Integer.parseInt(a_number,8);
		String l_newNumber = Integer.toHexString(number);
				
		return l_newNumber;
	}
	
	public String convertOCTtoDEC(String a_number)
	{
		int number =  Integer.parseInt(a_number,8);
		String l_newNumber = Integer.toString(number);
				
		return l_newNumber;
	}
	
	public String convertOCTtoBIN(String a_number)
	{
		int number =  Integer.parseInt(a_number,8);
		String l_newNumber = Integer.toBinaryString(number);
				
		return l_newNumber;
	}
	
	
	
	
	
	
	


}
